<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <button>触发ajax</button>
    <button>POST请求</button>
    <p></p>
    <script>
        var btn1 = document.getElementsByTagName("button")[0];
        var btn2 = document.getElementsByTagName("button")[1];
        var pBox = document.getElementsByTagName("p")[0];
        var xhr = new XMLHttpRequest();

        btn1.onclick = function () {
            var url = "/ajax?id=123";
            xhr.open("GET",url,true);
            xhr.send(null);
        }

        btn2.onclick = function () {
            var url = "/ajax";
            xhr.open("POST",url,true);
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xhr.send("name=xxx&id=321");
        }

        xhr.onreadystatechange = function (ev) {
            if(xhr.readyState==4){
                if(xhr.status==200) {
                    var mess = xhr.responseText;
                    pBox.innerHTML = mess;
                }else{
                    //响应失败的情况
                }
            }
        }

    </script>
</body>
</html>